﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace iHub
{
    public partial class importedfile : Form
    {
        private DataTable contents = new DataTable();
        private Boolean t;
        private AssignmentManager assm = new AssignmentManager();
        private string p = "";
        private string table = "";
        public static string batchno;
        public static int eid = 0;
        public static string ename = "";

        public importedfile(string path, Boolean type, string tablename)
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Form_Closing);
            t = type;
            p = path;
            table = tablename;
            LoadData(path,tablename);
        }

        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                documentselect.form.Enabled = true;
            }
        }

        private void LoadData(string path,string tablename)
        {
            try
            {
                if (System.IO.File.Exists(path))
                {
                    string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", path);
                    string query = String.Format("select * from [{0}]", tablename);
                    OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                    dataAdapter.Fill(contents);
                    dataGridView1.DataSource = contents;
                }
                else
                {
                    MessageBox.Show("No File is Selected");
                }
            }
            catch (Exception e)
            {
                MessageBox.Show("Error Detected : "+e.ToString(), "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void importdata_Click(object sender, EventArgs e)
        {
            string batch = p;
            int n = 0;
            string[] appended;

            try
            {
                appended = batch.Split('\\');
                n = appended.Count() - 1;
                batch = appended[n];
                batch = Regex.Replace(batch, "[^0-9]", "");

                if (t)
                {
                    if (batch.Equals(""))
                    {
                        batch = table;
                        batch = Regex.Replace(batch, "[^0-9]", "");

                        if (batch.Equals(""))
                        {
                            DialogResult msg = MessageBox.Show("There is no batch number specified in the file name or the table selected, all affected items will be listed under batch 0. Otherwise, select No to specify a batch number instead", "No batch number specified", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                            if (msg == DialogResult.Yes)
                            {
                                assm.importcontents(contents, batch);
                            }
                            else if (msg == DialogResult.No)
                            {
                                specbatch newbatch = new specbatch();
                                newbatch.ShowDialog(this);
                                newbatch.Dispose();
                                assm.importcontents(contents, batchno);
                            }
                        }
                        else
                        {
                            assm.importcontents(contents, batch);
                        }
                    }
                    else
                    {
                        assm.importcontents(contents, batch);
                    }
                }
                else
                {
                    DialogResult msg = MessageBox.Show("Include an employee? All items affected will be listed under the same employee. Employee must also be already encoded into the database before confirming","Include Employee",MessageBoxButtons.YesNo,MessageBoxIcon.Question);

                    if (msg == DialogResult.No)
                    {
                        if (batch.Equals(""))
                        {
                            batch = table;
                            batch = Regex.Replace(batch, "[^0-9]", "");

                            if (batch.Equals(""))
                            {
                                DialogResult msg2 = MessageBox.Show("There is no batch number specified in the file name or the table selected, all affected items will be listed under batch 0. Otherwise, select No to specify a batch number instead", "No batch number specified", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                                if (msg2 == DialogResult.Yes)
                                {
                                    assm.importcontents(contents,eid,ename,batch);
                                }
                                else if (msg2 == DialogResult.No)
                                {
                                    specbatch newbatch = new specbatch();
                                    newbatch.ShowDialog(this);
                                    newbatch.Dispose();
                                    assm.importcontents(contents, eid, ename, batchno);
                                }
                            }
                            else
                            {
                                assm.importcontents(contents, eid, ename, batch);
                            }
                        }
                        else
                        {
                            assm.importcontents(contents, eid, ename, batch);
                        }
                    }
                    else
                    {
                        selectemployee emp = new selectemployee();
                        emp.ShowDialog(this);
                        emp.Dispose();

                        if (batch.Equals(""))
                        {
                            batch = table;
                            batch = Regex.Replace(batch, "[^0-9]", "");

                            if (batch.Equals(""))
                            {
                                DialogResult msg2 = MessageBox.Show("There is no batch number specified in the file name or the table selected, all affected items will be listed under batch 0. Otherwise, select No to specify a batch number instead", "No batch number specified", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);

                                if (msg2 == DialogResult.Yes)
                                {
                                    assm.importcontents(contents, eid, ename, batch);
                                }
                                else if (msg2 == DialogResult.No)
                                {
                                    specbatch newbatch = new specbatch();
                                    newbatch.ShowDialog(this);
                                    newbatch.Dispose();
                                    assm.importcontents(contents, eid, ename, batchno);
                                }
                            }
                            else
                            {
                                assm.importcontents(contents, eid, ename, batch);
                            }
                        }
                        else
                        {
                            assm.importcontents(contents, eid, ename, batch);
                        }
                    }
                }
                MessageBox.Show("Excel contents successfully imported into database", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
                documentselect.form.Enabled = true;
                this.Dispose();
            }
            catch (Exception err)
            {
                MessageBox.Show("Error Detected, file format may be incorrect: "+err.Message, "Fatal", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
